﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace Liquid.Windows {
	public static class NativeUtilities {
		private const int WM_SETREDRAW = 0x000B;
		private const int WM_USER = 0x400;
		private const int EM_GETEVENTMASK = (WM_USER + 59);
		private const int EM_SETEVENTMASK = (WM_USER + 69);

		[DllImport("user32", CharSet = CharSet.Auto)]
		private extern static IntPtr SendMessage(IntPtr hWnd, int msg, int wParam, IntPtr lParam);

		public static IDisposable SuspendDrawing(this Control control) {
			var handle = control.Handle;
			SendMessage(handle, WM_SETREDRAW, 0, IntPtr.Zero);
			var eventMask = SendMessage(handle, EM_GETEVENTMASK, 0, IntPtr.Zero);
			return new CallbackDisposable(
				() => {
					SendMessage(handle, EM_SETEVENTMASK, 0, eventMask);
					SendMessage(handle, WM_SETREDRAW, 1, IntPtr.Zero);
					control.Invalidate();
				}
			);
		}
	}
}
